package com.lxk.weixin;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.alibaba.fastjson.JSONObject;

/**
 * 请求微信URL，拉取信息并解析为JSON对象
 * @author JohnSoybean
 *
 */
public class HttpUtil {
	 public static JSONObject doGet(String url) {
		 JSONObject demoJson = new JSONObject();
		 try {  
	         URL urlGet = new URL(url);  
	         HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();  
	         http.setRequestMethod("GET"); // 必须是get方式请求  
	         http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  
	         http.setDoOutput(true);  
	         http.setDoInput(true);  
	         /*System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒  
	         System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒 */
	         http.connect();  
	         InputStream is = http.getInputStream();  
	         int size = is.available();  
	         byte[] jsonBytes = new byte[size];  
	         is.read(jsonBytes);  
	         String message = new String(jsonBytes, "UTF-8"); 
	         demoJson = JSONObject.parseObject(message);  
	         is.close();  
	     } catch (Exception e) {  
	             e.printStackTrace();  
	     }  
	     return demoJson;
	 }
}
